<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .dis{
      background-color: rgb(64, 73, 75) !important;
    }
    .ani1-enter{
      transform: translateX(-100%);
      opacity: 0;
    }
    .ani1-enter-active,.ani1-leave-active{
      transition: all 500ms;
    }
    .ani1-leave-to{
      transform: translateX(100%);
      opacity: 0;
    }
  </style>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
</head>
<body>
  <div id="app" style="max-width: 600px; margin: 50px auto">
    <div class="row header">
      <div class="col-md-10 col-sm-10">
        <input v-model.trim="todoTxt" type="text" class="form-control">
      </div>
      <div class="col-md-2 col-sm-2">
        <button class="btn btn-success" @click="addTodo">增加</button>
      </div>
    </div>
    <div class="body" style="margin-top:20px">
      <ul class="list-group">
        <li v-if="!todos.length" class="list-group-item text-center">
          暂无代办事项
        </li>
        <transition-group name="ani1">
          <li class="list-group-item clearfix" v-for="(todo,index) in todos" :key="index">
            <span>{{todo.content}}</span>
            <div class="pull-right">
              <button 
                
                @click="setCompleted(index)"
                :class="['btn', 'btn-primary', 'btn-xs', {
                  dis: todo.isCompleted
                }]">
                {{ todo.isCompleted?'已完成':'未完成' }}
              </button>
              <button 
                @click="delTodo(index)"
                class="btn btn-danger btn-xs">删除</button>
                {{index}}
            </div>
          </li>
        </transition-group>
    </ul>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        todoTxt:'',
        todos: [
          
        ]
      },
      methods: {
        addTodo(){
          if(this.todoTxt === ''){
            return;
          }
          // todos增加一条
          this.todos.push({
            content: this.todoTxt,
            isCompleted: false
          })
          // 清除 输入框value 方便增加下一条
          this.todoTxt = '';
        },
        delTodo(index){
          // 删除todo
          if(confirm('您确认删除吗?')){
            this.todos.splice(index, 1);
          }
        },
        setCompleted(index){
          // 点击已完成
          this.todos[index].isCompleted = !this.todos[index].isCompleted;
        }
      }
    })
  </script>
</body>
</html>